<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\Delete;
use App\Admin\Actions\Post\Replicate;
use App\Model\Batch;
use App\Model\Sncode;
use App\Uitls\EncryptionKohana;
use App\Utils\RandomHb;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class BatchController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Batch';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Batch());

        $grid->column('id', __('Id'))->sortable();
        $grid->column('name', __('批次名称'));
        $grid->column('sum', __('产品总数量'));
        $grid->column('sum_rmb', __('红包总金额'));
        $grid->column('hb_mix', __('红包最小金额'));
        $grid->column('comment', __('备注'));
//        $grid->column('created_at', __('Created at'));
//        $grid->column('updated_at', __('Updated at'));
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            $actions->add(new Delete);

        });
        $grid->disableBatchActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Batch::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('批次名称'));
        $show->field('sum', __('产品总数量'));
        $show->field('sum_rmb', __('红包总金额'));
        $show->field('hb_mix', __('红包最小金额'));
        $show->field('comment', __('备注'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Batch());

        $form->text('name', __('批次名称'))->rules('required');
        $form->number('sum', __('产品总数量'))->rules('required');
        $form->currency('hb_mix', __('红包最小金额'))->rules('required');
        $form->currency('sum_rmb', __('红包总金额'))->rules('required');
        $form->text('comment', __('备注'));


        //保存前回调
        $form->saving(function (Form $form) {
            //...
            $sum_rmb=$form->sum_rmb;
            $sum= $form->sum;
            $mix=$form->hb_mix;
            $sums=$sum*$mix;
            info($sums-$sum_rmb<0);
            if ($sums<$sum_rmb!=1){
                $error = new MessageBag([
                    'title'   => '保存失败！',
                    'message' => '产品数量和最小红包金额乘积应该大于红包总金额！',
                ]);
                return back()->with(compact('error'));
            }

        });

        $form->saved(function (Form $form) {
            $id=$form->model()->id;
            $sum_rmb=$form->model()->sum_rmb;
            $sum=$form->model()->sum;
            $min=$form->model()->hb_mix;
            $hb=new RandomHb($sum_rmb,$sum,$min);
            $rmbs=$hb->handle();
            foreach ( $rmbs as $rmb){
               $sn=[
                   'sn'=>EncryptionKohana::random_string(15),
                   'rmb'=>$rmb,
                   'batch_id'=>$id
               ];
               $sns[]=$sn;
            }
              DB::table('sncodes')->insert($sns);
        });

        return $form;
    }
}
